﻿using Nemerle.Collections;
using Nemerle.Imperative;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;

using Nemerle2.Compiler;
using MSCci = Microsoft.Cci;

namespace MetadataReader.CCI
{
  class CciCustomAttributes : ICustomAttributes
  {
    public this(provider : CciMetadataProvider, attributes : Seq[MSCci.ICustomAttribute])
    {
      mutable names = [], attrs = [];

      foreach (attr in attributes)
      {
        def attrWrapper = CciCustomAttribute(provider, attr);

        attrs ::= attrWrapper;
        names ::= Utils.GetFullName(attr.Type);
      }

      CustomAttributeNames = names;
      CustomAttributes     = attrs;
    }

    public CustomAttributeNames : list[string]            { get; private set;}
    public CustomAttributes     : list[ICustomAttribute]  { get; private set; }

    public IsCustomAttributeDefined(attributeType : ITypeInfo)  : bool
    {
      foreach (attr when attr.Type.Equals(attributeType) in CustomAttributes)
        return true;

      false
    }

    public IsCustomAttributeDefined(attributeFullName : string) : bool
    {
      foreach (name when name == attributeFullName in CustomAttributeNames)
        return true;

      false
    }

    public CustomAttribute[attributeType : ITypeInfo]  : list[ICustomAttribute]
    {
      get { CustomAttributes.RevFilter(attr => attr.Type.Equals(attributeType)) }
    }
    public CustomAttribute[attributeFullName : string] : list[ICustomAttribute]
    {
      get { CustomAttributes.RevFilter(attr => attr.FullName == attributeFullName) }
    }
  }
}
